import { initRouter } from '@/router'
import { defineStore } from 'pinia'

export const useMenusStore = defineStore('menus', {
  state: () => ({
    menusRoutes: []
  }),
  getters: {
    menus() {
      return filterRouter(this.menusRoutes)
    }
  },
  actions: {
    // 保存权限路由菜单
    SAVE_MENUS(data) {
      localStorage.setItem('authMenus', JSON.stringify(data))

      this.INIT_MENUS()
    },
    INIT_MENUS() {
      this.menusRoutes = initRouter()
    }
  }
})

function filterRouter(routesArr) {
  return routesArr.filter((r) => {
    r.children &&= filterRouter(r.children)
    return r.meta?.showLink
  })
}
